﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(Format.Native)]
public struct MediaRect {

    private double avg;
    private double compSum;
    private int count;

    public void Init() { avg = 0; }

    public void Accumulate(Recta v) {
        if (!v.IsNull) {
            count += 1;
            compSum += v.Comprimento;
        }
    }

    public void Merge(MediaRect m) {
        count += m.count;
        compSum += m.compSum;
    }

    public Double Terminate() {
        return avg = compSum / count;
    }
}
